package thesis.bean;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.xml.bind.ValidationException;

import thesis.database.User;
import thesis.javaclass.SystemModel;

@ManagedBean(name = "userAdmin")
@SessionScoped
public class UserAdminBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private User user;
	private User editedUser;
	private List<User> users;

	private String query;
	private String username;

	private boolean hasUser;
	private boolean addBool;
	private boolean editBool;

	public boolean isHasUser() {
		return hasUser;
	}

	public User getEditedUser() {
		return editedUser;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public void setEditedUser(User editedUser) {
		this.editedUser = editedUser;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public boolean isAddBool() {
		return addBool;
	}

	public boolean isEditBool() {
		return editBool;
	}

	public UserAdminBean() {
		users = SystemModel.getAllUser();
		user = new User();
		addBool = false;
		editBool = false;

		if (users.size() > 0)
			hasUser = true;
		else
			hasUser = false;
	}

	public void delete() {
		FacesContext context = FacesContext.getCurrentInstance();

		if (editedUser == null)
			return;
		else {
			int ok = SystemModel.deleteUser(editedUser.getUserId());
			if (ok > 0) {
				context.addMessage(null, new FacesMessage("Deleted "
						+ editedUser.getUserName() + " successfully"));
				editedUser = null;
				users = null;
				users = SystemModel.getAllUser();
			} else {
				context.addMessage(null, new FacesMessage("Deleted "
						+ editedUser.getUserName() + " failing"));
			}
		}

		searchByQuery();
	}

	public void addClick() {
		FacesContext context = FacesContext.getCurrentInstance();

		if (addBool == true) {
			context.addMessage(null, new FacesMessage(
					"Finish adding form below first!"));
			return;
		}

		user = new User();
		addBool = true;
	}

	public void add() {
		user.setUserName(user.getUserName().toLowerCase());
		int ok = SystemModel.addUser(user);
		user = null;
		FacesContext context = FacesContext.getCurrentInstance();

		if (ok > 0) {
			addBool = false;
			users = null;
			users = SystemModel.getAllUser();
			context.addMessage(null, new FacesMessage(
					"Created user successfully"));
		} else
			context.addMessage(null, new FacesMessage("Created user failling"));
	}

	public void cancelAdd() {
		FacesContext context = FacesContext.getCurrentInstance();

		user = null;
		addBool = false;
		context.addMessage(null, new FacesMessage("Cancel adding!"));
	}

	public void viewDetail() {

	}

	public void edit() {
		/*
		 * ; int id = getIdCode(context);
		 * 
		 * editedUser = SystemModel.getUserById(id);
		 */
		if (editedUser == null)
			return;

		username = editedUser.getUserName();
		editBool = true;
		System.out.println(editedUser.getBirthDay());
	}

	public void cancelEdit() {
		FacesContext context = FacesContext.getCurrentInstance();

		context.addMessage(null, new FacesMessage(
				"Cancel editing for account: " + editedUser.getUserName()));
		editedUser = null;
		editBool = false;
	}

	public void updateEdit() {
		FacesContext context = FacesContext.getCurrentInstance();

		editedUser.setUserName(editedUser.getUserName().toLowerCase());
		editedUser.setLastUpdate(new Timestamp(new Date().getTime()));
		int ok = SystemModel.updateUser(editedUser);

		if (ok > 0) {
			editBool = false;
			context.addMessage(null, new FacesMessage(
					"Updated user successfully"));
		} else
			context.addMessage(null, new FacesMessage("Updated user failling"));
	}

	public void searchByQuery() {
		System.out.println(query);
		if (query == null)
			return;
		
		if (query.trim().equals(""))
			users = SystemModel.getAllUser();
		else
			users = SystemModel.getListUserByName(query);

		if (users.size() == 0 || users == null)
			hasUser = false;
		else
			hasUser = true;
	}

	public int getIdCode(FacesContext context) {
		Map<String, String> params = context.getExternalContext()
				.getRequestParameterMap();

		return Integer.parseInt(params.get("id"));
	}

	public void checkUserName(FacesContext fc, UIComponent u, Object value) {
		String temp = value.toString().trim();
		if (SystemModel.checkNameOfUserExist(temp) != null)
			throw new ValidatorException(new FacesMessage(temp
					+ " has already exist"));
		else if (!temp.matches("^[a-z0-9_-]{3,15}$")) {
			throw new ValidatorException(
					new FacesMessage("Username is invalid"));
		}
	}

	public void checkUserNameEdit(FacesContext fc, UIComponent u, Object value) {
		String temp = value.toString().toLowerCase();
		if (!temp.equals(username)) {
			checkUserName(fc, u, value);
		}
	}
}
